package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSParameters;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchParametersRequest;
import com.ops.schema.ns.system.SearchParametersResponse;
import com.ops.schema.ns.system.UpdateParametersRequest;
import com.ops.schema.ns.system.UpdateParametersResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class ParameterMBean {

	private String paramName;
	private String paramType;
	private String activeFrom;
	private String activeTo;
	private String mode;
	private String delflag;
	private String saveStatus;
	private List<SelectItem> typeList;
	private List<OPSLookUps> oMaster;
	private List<OPSParameters> opsParameters;
	private OPSParameters opsParams;
	private Map<String, OPSParameters> opsMap;

	public void searchParameters() {
		System.out.println("Inside searchParameters");
		SearchParametersRequest paramRequest = new SearchParametersRequest();
		paramRequest.setName(paramName);
		paramRequest.setType(paramType);
		paramRequest.setActiveFrom(activeFrom);
		paramRequest.setActiveTo(activeTo);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		paramRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchParametersResponse parametersResponse = OnlinePayrollSystemServiceClient
					.searchParameter(paramRequest);
			setOpsParameters(parametersResponse.getOPSParameters());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetParameters() {
		setParamName(null);
		setParamType(null);
		setActiveFrom(null);
		setActiveTo(null);
	}

	public void saveParameters(ActionEvent event) {

		UpdateParametersRequest updateRequest = new UpdateParametersRequest();
		OPSParameters opsParameters = new OPSParameters();
		opsParameters.setName(opsParams.getName());
		opsParameters.setKey(opsParams.getKey());
		opsParameters.setId(opsParams.getId());
		opsParameters.setValue(opsParams.getValue());
		opsParameters.setType(opsParams.getType());
		opsParameters.setOperationFlag("U");
		opsParameters.setActiveFrom(opsParams.getActiveFrom());
		opsParameters.setActiveTo(opsParams.getActiveTo());
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		if(opsParameters.getId() != null)
			uditInfo.setAction("U");
			else
			uditInfo.setAction("I");	
			uditInfo.setObjectType(getMenuId());
		opsParameters.setOPSRecordAuditInfo(uditInfo);

		updateRequest.getOPSParameters().add(opsParameters);
		try {
			UpdateParametersResponse response = persistData(updateRequest);
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchParameters();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void delParameters(ActionEvent event) {
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateParametersRequest updateRequest = new UpdateParametersRequest();
				String key = mode.replace("Del_", "").trim();
				OPSParameters parameters = new OPSParameters();
				parameters.setId(key);
				parameters.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setUpdatedSession(getSessionId());
				uditInfo.setAction("D");	
				uditInfo.setObjectType(getMenuId());
				parameters.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSParameters().add(parameters);
				try {
					persistData(updateRequest);
					searchParameters();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}

	}

	public OPSParameters addParameters() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsParameters() != null) {
				opsMap = new HashMap<String, OPSParameters>();
				for (OPSParameters parameters : getOpsParameters()) {
					System.out.println("Inside" + parameters.getId());
					opsMap.put(parameters.getId(), parameters);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsParams = new OPSParameters();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				opsParams = opsMap.get(key);
			}
		}
		System.out.println("Inside addParameters:::" + mode);
		return opsParams;
	}

	public UpdateParametersResponse persistData(
			UpdateParametersRequest parametersRequest) throws ServiceException {
		UpdateParametersResponse parametersResponse = OnlinePayrollSystemServiceClient
				.saveParameter(parametersRequest);
		return parametersResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	public List<OPSLookUps> getoMaster() {

		if (oMaster == null) {
			oMaster = new ArrayList<OPSLookUps>();
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("PARAMETER-TYPE");
			OPSLookUps oMasterObj = new OPSLookUps();
			oMasterObj.setCode("");
			oMasterObj.setName("--Select--");
			oMaster.add(oMasterObj);
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps OPSLookUps : masterResponse.getOPSLookUps()) {
					OPSLookUps paramMaster = new OPSLookUps();
					paramMaster.setCode(OPSLookUps.getCode());
					paramMaster.setName(OPSLookUps
							.getName());
					oMaster.add(paramMaster);
				}

			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return oMaster;
	}

	public void setoMaster(List<OPSLookUps> oMaster) {
		this.oMaster = oMaster;
	}

	public List<SelectItem> getTypeList() {
		if (typeList == null) {
			typeList = new ArrayList<SelectItem>();
			List<OPSLookUps> masters = getoMaster();
			for (OPSLookUps master : masters) {

				SelectItem item = new SelectItem(master.getCode(),
						master.getName());
				typeList.add(item);

			}

		}
		return typeList;
	}

	public void setTypeList(List<SelectItem> typeList) {
		this.typeList = typeList;
	}

	public List<OPSParameters> getOpsParameters() {
		return opsParameters;
	}

	public void setOpsParameters(List<OPSParameters> opsParameters) {
		this.opsParameters = opsParameters;
	}

	public String getParamName() {
		return paramName;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	public String getParamType() {
		return paramType;
	}

	public void setParamType(String paramType) {
		this.paramType = paramType;
	}

	public String getActiveFrom() {
		return activeFrom;
	}

	public void setActiveFrom(String activeFrom) {
		this.activeFrom = activeFrom;
	}

	public String getActiveTo() {
		return activeTo;
	}

	public void setActiveTo(String activeTo) {
		this.activeTo = activeTo;
	}

	private List<OPSParameters> parameters;

	public List<OPSParameters> getParameters() {
		return parameters;
	}

	public void setParameters(List<OPSParameters> parameters) {
		this.parameters = parameters;
	}

	public OPSParameters getOpsParams() {
		return opsParams;
	}

	public void setOpsParams(OPSParameters opsParams) {
		this.opsParams = opsParams;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public Map<String, OPSParameters> getOpsMap() {
		return opsMap;
	}

	public void setOpsMap(Map<String, OPSParameters> opsMap) {
		this.opsMap = opsMap;
	}

	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public String getSaveStatus() {
		return saveStatus;
	}

	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}

}
